#include "diplay/simpledelegate.h"
#include "common.h"
#include <QPainter>


void SimpleDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //保存状态
    painter->save();

    if(option.state & QStyle::State_Selected)
    {
        painter->fillRect(option.rect, option.palette.highlight());
    }
    else{
        if(index.row()%2==0)
        {
            painter->fillRect(option.rect, option.palette.base());
        }
        else{
            painter->fillRect(option.rect, option.palette.background());
        }
    }

    //绘制自定义内容
    QString text = index.data(Qt::DisplayRole).toString();
    if(index.row()%2==0)
    {
        painter->drawText(option.rect, Qt::AlignLeft|Qt::AlignVCenter,text);
    }
    else{
        painter->drawText(option.rect, Qt::AlignRight|Qt::AlignVCenter,text);
    }


    //恢复状态
    painter->restore();
}

QSize SimpleDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    //设置项的高度为50
    qDebug()<<" row:"<<index.row()<<"  "<<option.rect<<endl;
    //return QSize(100,50);
   return QSize(option.rect.width(),50);
}
